// 应用管理器，负责全局状态和服务的管理
export class AppManager {
  private static instance: AppManager;
  private serviceInstances: Map<string, any> = new Map();
  private appState: Record<string, any> = {};

  // 单例模式
  public static getInstance(): AppManager {
    if (!AppManager.instance) {
      AppManager.instance = new AppManager();
    }
    return AppManager.instance;
  }

  // 获取服务实例（单例模式）
  public getService<T>(serviceName: string, factory: () => T): T {
    if (!this.serviceInstances.has(serviceName)) {
      this.serviceInstances.set(serviceName, factory());
    }
    return this.serviceInstances.get(serviceName) as T;
  }

  // 设置全局状态
  public setState(key: string, value: any): void {
    this.appState[key] = value;
  }

  // 获取全局状态
  public getState(key: string): any {
    return this.appState[key];
  }

  // 清除全局状态
  public clearState(key?: string): void {
    if (key) {
      delete this.appState[key];
    } else {
      this.appState = {};
    }
  }

  // 初始化应用配置
  public initialize(): void {
    console.log('应用框架初始化完成');
    // 可以在这里添加其他初始化逻辑
    this.setState('initialized', true);
    this.setState('version', '1.0.0');
  }

  // 清理资源
  public cleanup(): void {
    this.serviceInstances.clear();
    this.clearState();
    console.log('应用框架资源清理完成');
  }

  // 获取应用信息
  public getAppInfo(): { version: string; initialized: boolean } {
    return {
      version: this.getState('version') || 'unknown',
      initialized: this.getState('initialized') || false
    };
  }
}